package clue.editor;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import objects.Character;
import objects.ClueCheckBox;
import objects.ClueComboBox;
import objects.ClueRadio;

public class PlayerPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private ButtonGroup buttonGroup;
	private JButton removeButton;
	private PlayersPanel containingPanel; 
	private JSpinner order;
	
	/**
	 * Create the panel.
	 */
	public PlayerPanel(PlayersPanel containingPanel) {
		setPreferredSize(new Dimension(352, 390));
		setSize(new Dimension(352, 374));
		setMinimumSize(new Dimension(352, 374));
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("name:");
		lblNewLabel.setBounds(50, 25, 46, 14);
		add(lblNewLabel);
		
		textField = new JTextField();
		textField.getDocument().addDocumentListener(getDocListener());
		
		textField.setBounds(87, 22, 86, 20);
		add(textField);
		textField.setColumns(10);
		
		buttonGroup = new ButtonGroup();
		
		JRadioButton rdbtnNewRadioButton = new ClueRadio("HUMAN");
		rdbtnNewRadioButton.setBounds(261, 65, 78, 23);
		add(rdbtnNewRadioButton);
		buttonGroup.add(rdbtnNewRadioButton);
		
		JRadioButton rdbtnNewRadioButton_1 = new ClueRadio("COMPUTER");
		rdbtnNewRadioButton_1.setBounds(173, 65, 86, 23);
		add(rdbtnNewRadioButton_1);
		buttonGroup.add(rdbtnNewRadioButton_1);
		
		JComboBox<Character> comboBox_1 = new ClueComboBox<Character>();
		comboBox_1.setModel(new DefaultComboBoxModel<Character>(Character.values()));
		comboBox_1.setBounds(6, 66, 136, 20);
		add(comboBox_1);
		
		JLabel lblWeapons = new JLabel("Weapons:");
		lblWeapons.setBounds(10, 121, 60, 14);
		add(lblWeapons);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(-15, 97, 372, 2);
		add(separator);
		
		JCheckBox chckbxNewCheckBox = new ClueCheckBox("CANDLESTICK");
		
		chckbxNewCheckBox.setBounds(6, 141, 97, 23);
		add(chckbxNewCheckBox);
		
		JCheckBox checkBox = new ClueCheckBox("LEAD_PIPE");
		checkBox.setBounds(6, 167, 97, 23);
		add(checkBox);
		
		JCheckBox checkBox_1 = new ClueCheckBox("ROPE");
		checkBox_1.setBounds(6, 193, 97, 23);
		add(checkBox_1);
		
		JCheckBox checkBox_2 = new ClueCheckBox("DAGGER");
		checkBox_2.setBounds(6, 219, 97, 23);
		add(checkBox_2);
		
		JCheckBox chckbxRevolver = new ClueCheckBox("REVOLVER");
		chckbxRevolver.setBounds(6, 245, 97, 23);
		add(chckbxRevolver);
		
		JCheckBox chckbxWrench = new ClueCheckBox("WRENCH");
		chckbxWrench.setBounds(6, 271, 97, 23);
		add(chckbxWrench);
		
		JLabel lblCharacters = new JLabel("Characters:");
		lblCharacters.setBounds(113, 121, 60, 14);
		add(lblCharacters);
		
		JCheckBox chckbxNewCheckBox_1 = new ClueCheckBox("MISS_SCARLETT");
		chckbxNewCheckBox_1.setBounds(113, 141, 109, 23);
		add(chckbxNewCheckBox_1);
		
		JCheckBox chckbxColonelmustard = new ClueCheckBox("COLONEL_MUSTARD");
		chckbxColonelmustard.setBounds(113, 167, 125, 23);
		add(chckbxColonelmustard);
		
		JCheckBox chckbxMrswhite = new ClueCheckBox("MRS_WHITE");
		chckbxMrswhite.setBounds(113, 193, 97, 23);
		add(chckbxMrswhite);
		
		JCheckBox chckbxReverendgreen = new ClueCheckBox("REVEREND_GREEN");
		chckbxReverendgreen.setBounds(113, 219, 125, 23);
		add(chckbxReverendgreen);
		
		JCheckBox chckbxMrspeacock = new ClueCheckBox("MRS_PEACOCK");
		chckbxMrspeacock.setBounds(113, 245, 109, 23);
		add(chckbxMrspeacock);
		
		JCheckBox chckbxProffesorplum = new ClueCheckBox("PROFFESOR_PLUM");
		chckbxProffesorplum.setBounds(113, 271, 125, 23);
		add(chckbxProffesorplum);
		
		JLabel lblRooms = new JLabel("Rooms:");
		lblRooms.setBounds(247, 121, 46, 14);
		add(lblRooms);
		
		JCheckBox chckbxNewCheckBox_2 = new ClueCheckBox("KITCHEN");
		chckbxNewCheckBox_2.setBounds(242, 141, 97, 23);
		add(chckbxNewCheckBox_2);
		
		JCheckBox chckbxBallroom = new ClueCheckBox("BALLROOM");
		chckbxBallroom.setBounds(242, 167, 97, 23);
		add(chckbxBallroom);
		
		JCheckBox chckbxConservatory = new ClueCheckBox("CONSERVATORY");
		chckbxConservatory.setBounds(242, 193, 109, 23);
		add(chckbxConservatory);
		
		JCheckBox chckbxBilliardroom = new ClueCheckBox("BILLIARD_ROOM");
		chckbxBilliardroom.setBounds(242, 219, 115, 23);
		add(chckbxBilliardroom);
		
		JCheckBox chckbxLibrary = new ClueCheckBox("LIBRARY");
		chckbxLibrary.setBounds(242, 245, 97, 23);
		add(chckbxLibrary);
		
		JCheckBox chckbxStudy = new ClueCheckBox("STUDY");
		chckbxStudy.setBounds(242, 271, 97, 23);
		add(chckbxStudy);
		
		JCheckBox chckbxHall = new ClueCheckBox("HALL");
		chckbxHall.setBounds(242, 297, 97, 23);
		add(chckbxHall);
		
		JCheckBox chckbxLounge = new ClueCheckBox("LOUNGE");
		chckbxLounge.setBounds(242, 323, 97, 23);
		add(chckbxLounge);
		
		JCheckBox chckbxDiningroom = new ClueCheckBox("DINING_ROOM");
		chckbxDiningroom.setPreferredSize(new Dimension(250, 350));
		chckbxDiningroom.setBounds(242, 349, 102, 23);
		add(chckbxDiningroom);
		
		this.containingPanel = containingPanel;
		
		removeButton = new JButton("Remove");
		removeButton.setBounds(133, 366, 89, 23);
		removeButton.addActionListener(getRemovePlayerAction());
		add(removeButton);
		
		JLabel lblNewLabel_1 = new JLabel("order:");
		lblNewLabel_1.setBounds(183, 25, 36, 14);
		add(lblNewLabel_1);
		
		order = new JSpinner();
		order.setModel(new SpinnerNumberModel(1, 1, 6, 1));
		order.setBounds(230, 22, 29, 20);
		order.addChangeListener(getChangeOrderListener());
		add(order);

	}
	
	public void clearCardSelection()
	{
		for (Component component : getComponents()) {
			if (component instanceof JCheckBox)
			{
				((JCheckBox) component).setSelected(false);
			}
		}
	}
	
	public ActionListener getRemovePlayerAction() {
		return new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				containingPanel.removePlayer(e);
				ClueFrame.getInstance().addEdit();
			}
		};
		
	}
	
	public ChangeListener getChangeOrderListener() {
		return new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				if (!GlobalHistory.getInstance().isDisabled())
				ClueFrame.getInstance().addEdit();

			}
		};
	}
	

	public DocumentListener getDocListener()
	{
		return new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent e) {
				ClueFrame.getInstance().addEdit();
				
			}
			
			@Override
			public void insertUpdate(DocumentEvent e) {
				ClueFrame.getInstance().addEdit();
				
			}
			
			@Override
			public void changedUpdate(DocumentEvent e) {
				ClueFrame.getInstance().addEdit();
				
			}
		};
	}
	
	public void setOrder(int orderNum) {
		order.setValue(orderNum);
	}
	
	public int getOrder() {
		return (int) order.getValue();
	}
}